import { DefaultFooter, getMenuData, getPageTitle } from '@ant-design/pro-layout';
import DocumentTitle from 'react-document-title';
import Link from 'umi/link';
import React from 'react';
import { connect } from 'dva';
import { formatMessage } from 'umi-plugin-react/locale';
import SelectLang from '@/components/SelectLang';
import logo from '../assets/logo.svg';
import styles from './UserLayout.less';
import Router from "umi/router";
import {fakeAccountLogin} from "@/services/login";
const forge = require('node-forge');

class UserLayout extends React.Component {
  constructor(props) {
    super(props);
    const loginMsg = window.location.hash.indexOf("?") !== -1 ? window.location.hash.split("?")[1].split("&") : ''
    this.state = {
      currentHeight: document.body.offsetHeight,
      currentWidth: document.body.offsetWidth,
      isNoLogin:loginMsg.length < 2 || loginMsg[1].split("=")[1] !== 'true' ? false : true
    }
  }
  componentDidMount() {
    var tenantUrl = ''
    const loginMsg = window.location.hash.indexOf("?") !== -1 ? window.location.hash.split("?")[1].split("&") : ''
    if (loginMsg.length < 2 || loginMsg[1].split("=")[1] !== 'true'){
      this.setState({isNologin:false})
      if(localStorage.getItem("tenantUrl")){
        if (window.location.hash.indexOf("tenantUrl") !== -1 && loginMsg[0].split("=")[1] !== localStorage.getItem("tenantUrl")){
          tenantUrl = loginMsg[0].split("=")[1]
        }else {
          tenantUrl = localStorage.getItem("tenantUrl")
        }
      }else {
        if (window.location.hash.indexOf("tenantUrl") !== -1){
          tenantUrl = loginMsg[0].split("=")[1]
        }else {
          tenantUrl = 'default'
        }
      }
      if (tenantUrl === 'null' || tenantUrl === null){
        tenantUrl = 'default'
      }
      Router.push(`/user/login?tenantUrl=` + tenantUrl);
    }else {
      //0-tenantUrl,1-isNologin,2-username,3-password
      this.doLogin({
        username: loginMsg[2].split("=")[1],
        password: loginMsg[3].split("=")[0]==='passwordMd5' ? loginMsg[3].split("=")[1] :this.md5pw(loginMsg[3].split("=")[1]),
        tenantUrl: loginMsg[0].split("=")[1],
      })
    }
  }

  md5pw(value) {
    let md = forge.md.md5.create();
    md.update(value);
    return md.digest().toHex();
  }

  doLogin(param) {
    const route = window.location.hash.split("router=")[1].replace('&','?')
    fakeAccountLogin(param).then(res => {
      if (res.retCode === '0') {
        const user = res.data.userVO;
        localStorage.setItem('antd-pro-authority',JSON.stringify(res.data.userVO));
        sessionStorage.setItem('user', JSON.stringify(user));
        sessionStorage.setItem('idToken', res.data.idToken);
        window.location.href = window.location.href.split('#')[0]+'#/'+route;
      }
    })
  }
  render() {
    const props = this.props
    const {
      route = {
        routes: [],
      },
    } = props;
    const { routes = [] } = route;
    const {
      children,
      location = {
        pathname: '',
      },
    } = props;
    const { breadcrumb } = getMenuData(routes);
    return (
      <DocumentTitle
        title={getPageTitle({
          pathname: location.pathname,
          breadcrumb,
          formatMessage,
          ...props,
        })}
      >
        {
          this.state.isNoLogin ? '' : <div className={styles.container}>
          <div className={styles.lang}>
            {/* <SelectLang /> */}
          </div>
          <div className={styles.content}>
            <div className={styles.top}>
              <div className={styles.header}>
                <Link to="/">
                  <img alt="logo" className={styles.logo} src={logo}/>
                  <span className={styles.title}>柯基数据</span>
                </Link>
              </div>
              <div className={styles.desc}>模型平台</div>
            </div>
            {children}
          </div>
        </div>}
      </DocumentTitle>
    );
  }
};

export default connect(({ settings }) => ({ ...settings }))(UserLayout);
